<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>命令模式</title>
  </head>
  <body>
    <p>执行命令时，发布者和执行者分开，中间加入命令对象，作为中转站</p>
    <script>
      class Receiver {
        exec() {
          console.log("执行");
        }
      }
      class Command {
        constructor(receiver) {
          this.receiver = receiver;
        }
        cmd() {
          console.log("触发命令");
          this.receiver.exec();
        }
      }
      class Invoker {
        constructor(command) {
          this.command = command;
        }
        invoke() {
          console.log("开始");
          this.command.cmd();
        }
      }

      // 士兵
      let soldier = new Receiver();
      // 小号手
      let trumpeter = new Command(soldier);
      // 将军
      let general = new Invoker(trumpeter);

      general.invoke();
    </script>
  </body>
</html>
